﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataAccess;
using System.IO;

namespace Manager
{
    public partial class AddService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitEditor();
            if (!Page.IsPostBack)
            {
                imgThumb.ImageUrl = Config.Global.Settings.ImagePath + "NoImage.gif";
            }
        }

        #region InitEditor
        protected void InitEditor()
        {
            editorContent.UIColor = System.Drawing.Color.DarkGreen;
            editorContent.config.toolbar = new object[]
			{
				new object[] { "Source", "-", "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" },
				new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote", "CreateDiv" },
				new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
				new object[] { "BidiLtr", "BidiRtl" },
				new object[] { "Link", "Unlink", "Anchor" },
				new object[] { "Image", "Flash", "Table", "HorizontalRule", "Smiley", "SpecialChar", "PageBreak", "Iframe" },
				"/",
				new object[] { "Styles", "Format", "Font", "FontSize" },
				new object[] { "TextColor", "BGColor" }
			};

            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(editorContent);
        }
        #endregion

        #region Check Input
        private bool CheckInput(string title, string url)
        {
            if (title == "")
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Bạn chưa nhập Tên Dịch vụ";
                txtTitle.Focus();
                return false;
            }

            if (url == "")
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Bạn chưa nhập Link Dịch vụ.";
                txtURL.Focus();
                return false;
            }

            ServiceBusiness serviceBusiness = new ServiceBusiness();
            int check = serviceBusiness.CheckExistsServiceURL(url);
            if (check != 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Link dịch vụ này đã tồn tại. Mời bạn chọn Link khác.";
                txtURL.Focus();
                return false;
            }
            return true;
        }
        #endregion

        #region Add Service
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string title = txtTitle.Text.Trim();
            string desc = txtDesc.Text.Trim();
            string content = editorContent.Text;
            string url = txtURL.Text.Trim();

            string image = hiddenImg.Value;
            int pos = image.LastIndexOf("/");
            image = image.Substring(pos + 1);

            if (CheckInput(title, url))
            {
                ServiceInfo serviceInfo = new ServiceInfo();

                serviceInfo.SiteID = Config.Global.Settings.SiteID;
                serviceInfo.ServiceName = title;
                serviceInfo.ServiceImage = image;
                serviceInfo.ServiceContent = content;
                serviceInfo.ServiceDescription = desc;
                serviceInfo.ServiceURL = url;
                if (rbtActive.Checked)
                {
                    serviceInfo.ServiceStatus = true;
                } 
                else
                {
                    serviceInfo.ServiceStatus = false;
                }

                ServiceData.Insert(serviceInfo);
                Response.Redirect("ServiceManager.aspx");
            }
        }
        #endregion

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("ServiceManager.aspx");
        }

        protected void lblDel_Click(object sender, EventArgs e)
        {
            imgThumb.ImageUrl = Config.Global.Settings.ImagePath + "NoImage.gif";
            hiddenImg.Value = "";
        }
    }
}